package com.lvan.service.common;

import java.io.Serializable;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.lvan.bean.BaseException;
import com.lvan.bean.GoodsType;
import com.lvan.bean.Page;
import com.lvan.constant.ValidateRecord;
import com.lvan.dao.mapper.GoodsTypeMapper;
import com.lvan.service.AbstractService;
import com.lvan.service.interfaze.common.IGoodsTypeService;
import com.lvan.util.InputValidate;

@Service("goodsTypeService")
public class GoodsTypeService extends AbstractService implements IGoodsTypeService {
	@Autowired
	private GoodsTypeMapper goodsTypeMapper;
	private InputValidate<GoodsType> validator = new InputValidate<GoodsType>();
	@Override
	public void add(GoodsType o) throws BaseException {
		Set<ValidateRecord> constraintViolations = validator.validate(o,true);
        if (!constraintViolations.isEmpty()) {
            StringBuilder sb = new StringBuilder();
            for (ValidateRecord constraintViolation : constraintViolations) {
                sb.append(constraintViolation.getMessage()+"; ");
            }
            throw new BaseException("",REQUEST_PARAM_ERROR,sb.toString());
        }
        goodsTypeMapper.add(o);
	}

	@Override
	public void delete(Serializable ids) throws BaseException {
		if(ids!=null)
			goodsTypeMapper.delete(ids);
	}

	@Override
	public void update(GoodsType o) throws BaseException {
		Set<ValidateRecord> constraintViolations = validator.validate(o,false);
        if (!constraintViolations.isEmpty()) {
            StringBuilder sb = new StringBuilder();
            for (ValidateRecord constraintViolation : constraintViolations) {
                sb.append(constraintViolation.getMessage()+"; ");
            }
            throw new BaseException("",REQUEST_PARAM_ERROR,sb.toString());
        }
        goodsTypeMapper.update(o);
	}

	@Override
	public GoodsType getByID(Serializable id) {
		return goodsTypeMapper.getByID(id);
	}

	@Override
	public int count(GoodsType o) {
		return goodsTypeMapper.count(o);
	}

	@Override
	public List<GoodsType> query(GoodsType o, Page page) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("goodsType", o);
		params.put("page", page.getPageSql());
		params.put("orderBy", o.convertField2Column());
		page.setTotal(this.count(o));
        return goodsTypeMapper.query(params);
	}

}
